﻿using Dapper;
using System.Data;

namespace Cap.Testing
{
    public class DbContext
    {
        private readonly IDbConnection _connection;

        private IDbTransaction? _transaction;

        public DbContext(IDbConnection connection)
        {
            _connection = connection;
        }

        public void BeginTransaction()
        {
            _transaction = _connection.BeginTransaction();
        }

        public void CommitTransaction()
        {
            try
            {
                _transaction?.Commit();
            }
            finally
            {
                _transaction = null;
            }

        }

        public Task<int> ExecuteAsync(string sql, object param)
        {
            return _connection.ExecuteAsync(sql, param, _transaction);
        }
    }
}
